package guiPackage;


import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import javax.swing.JPanel;
import javax.swing.JTextField;

import systemPackage.LocalStrings;
import systemPackage.MainSystem;

import dataTypesPackage.DistanceBetweenLiman;
import dataTypesPackage.Liman;
import dataTypesPackage.Ulke;


public class GUI_NewLiman extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2615023347788979082L;
	private Liman liman;
	private JPanel portDataPage = new JPanel();
	private JPanel portDistancePage = new JPanel();

	JLabel portNameLabel = new JLabel(LocalStrings.getLocalString(11));
	JTextField portNameField = new JTextField();
	
	JLabel portCityLabel = new JLabel(LocalStrings.getLocalString(12));
	JTextField portCityField = new JTextField();
	
	JLabel portUlkeLabel = new JLabel(LocalStrings.getLocalString(13));
	JTextField portUlkeField = new JTextField();
	
	private JButton nextButton = new JButton(LocalStrings.getLocalString(14));
	private JLabel nextLimanLabel;
	private Liman nextLiman;
	private JTextField distanceField;
	private int currentLiman=1;
	List<DistanceBetweenLiman> distanceBetweenLiman = new ArrayList<DistanceBetweenLiman>();
	
	private JButton nextPortButton = new JButton(LocalStrings.getLocalString(17));

	public GUI_NewLiman(){
		createWindow();
	}
	
	public void createWindow(){
		//the beginning of the first page. This page will be used to fill the needed data to create a new port.
		this.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );

		portDataPage.setLayout(new GridLayout(4,2));
		
		portDataPage.add(portNameLabel);
		portDataPage.add(portNameField);
		
		portDataPage.add(portCityLabel);
		portDataPage.add(portCityField);
		
		portDataPage.add(portUlkeLabel);
		portDataPage.add(portUlkeField);
		
		if(MainSystem.getLimanCount() == currentLiman-2){					//because we create a 0th entry at the same time of 1st entry, and getLimanCount returns size - 1. we should say save not next at 0.
			nextButton.setText(LocalStrings.getLocalString(19));
		}
		
		ActionListener nextButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method calls for mainSystem to edit client
				Ulke portUlke = MainSystem.getUlkeByName(portUlkeField.getText());
				System.out.println("ulke " + portUlke.getName());
				// TODO make limanCalisaniID real. 
				liman = new Liman(1, portNameField.getText(), portCityField.getText(), portUlke);				// 1 is hardcoded because we don't have any real LimanCalisani
				if (MainSystem.isFirstLiman()){
					MainSystem.set(liman, Liman.class);
					dispose();
				}
				else {					
					nextWindow();
				}
			}
		};
		nextButton.addActionListener(nextButtonPress);
		portDataPage.add(nextButton);
		this.setSize(240, 200);
		this.add(portDataPage);
		
		
	}
	
	private void cleanNextWindow() {
		// This method is used to empty the objects that nextWindow uses.
		nextLiman = null;
		portDistancePage.remove(nextLimanLabel);
		portDistancePage.remove(distanceField);
		portDistancePage.remove(nextPortButton);
		nextLimanLabel = null;
		distanceField = null;
		nextPortButton = null;
		this.remove(portDistancePage);

		nextWindow();
		
	}
	
	public void nextWindow(){
		this.remove(portDataPage);
		
		nextLiman = (Liman) MainSystem.get(currentLiman, Liman.class);
		nextLimanLabel = new JLabel(nextLiman.getName() + LocalStrings.getLocalString(16));
		distanceField = new JTextField();
		distanceField.setColumns(20);
		if(MainSystem.getLimanCount() == currentLiman){
			nextPortButton = new JButton(LocalStrings.getLocalString(19));
		}
		else {
			nextPortButton = new JButton(LocalStrings.getLocalString(17));	
		}
		
		
		portDistancePage.add(nextLimanLabel);
		portDistancePage.add(distanceField);
		ActionListener nextPortButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method decides if the port needs a new distance entry, and if not, saves.
				DistanceBetweenLiman dbLiman = new DistanceBetweenLiman(liman, nextLiman, Integer.parseInt(distanceField.getText()));
				distanceBetweenLiman.add(dbLiman);
				if (MainSystem.getLimanCount()== currentLiman){
					MainSystem.saveNewLiman(liman, distanceBetweenLiman);
					dispose();
				}
				else {
					currentLiman++;
					cleanNextWindow();
				}
				
			}
		};
		nextPortButton.addActionListener(nextPortButtonPress);
		portDistancePage.add(nextPortButton);
		this.add(portDistancePage);
		this.setSize(240, 140);
		this.setVisible(false);
		this.setVisible(true);
	
	}
	
	

}
